<?php
class QuyetdinhttdoancbsController extends AppController {

	var $name = 'Quyetdinhttdoancbs';
	var $uses = array('Quyetdinhttdoancb','Canbo','Ttdoancb','Chidoansv');
	function index() {
		$this->Quyetdinhttdoancb->recursive = 0;
		$this->set('quyetdinhttdoancbs', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid quyetdinhttdoancb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('quyetdinhttdoancb', $this->Quyetdinhttdoancb->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Quyetdinhttdoancb->create();
			if ($this->Quyetdinhttdoancb->save($this->data)) {
				$this->Session->setFlash(__('The quyetdinhttdoancb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The quyetdinhttdoancb could not be saved. Please, try again.', true));
			}
		}
		$ttdoancbs = $this->Quyetdinhttdoancb->Ttdoancb->find('list');
		$canbos = $this->Quyetdinhttdoancb->Canbo->find('list');
		$this->set(compact('ttdoancbs', 'canbos'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid quyetdinhttdoancb', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Quyetdinhttdoancb->save($this->data)) {
				$this->Session->setFlash(__('The quyetdinhttdoancb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The quyetdinhttdoancb could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Quyetdinhttdoancb->read(null, $id);
		}
		$ttdoancbs = $this->Quyetdinhttdoancb->Ttdoancb->find('list');
		$canbos = $this->Quyetdinhttdoancb->Canbo->find('list');
		$this->set(compact('ttdoancbs', 'canbos'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for quyetdinhttdoancb', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Quyetdinhttdoancb->delete($id)) {
			$this->Session->setFlash(__('Quyetdinhttdoancb deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Quyetdinhttdoancb was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function denghi($chidoan_id= null)
	{
		if(!$chidoan_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
		if(!empty($this->data))
		{
			//luu ketnapdvs
			$record['Ttdoancb']['ngayttdoan']=date('Y-m-d');
			$record['Ttdoancb']['chidoansv_id']=$chidoan_id;
			$kn = $this->Ttdoancb->find('list',array(
												'conditions'=>array('chidoansv_id'=>$chidoan_id,
																'ngayttdoan'=>$record['Ttdoancb']['ngayttdoan'])));
			if(!empty($kn))
			{
				$idttd= key($kn);
			}
			else
			{
				$this->Ttdoancb->create();				
				if($this->Ttdoancb->saveAll($record))
				{
					$idttd = $this->Ttdoancb->id;
				}
				else
				{
					$this->Session->setFlash(__("Lỗi khi Lưu đề nghị.",true));
					$this->redirect(array('denghi',$chidoan_id));
				}
			}
			$this->data['Quyetdinhttdoancb']['ttdoancb_id']=$idttd;
			$this->data['Quyetdinhttdoancb']['duyet']=0;
			foreach ($this->data['Quyetdinhttdoancb']['checkbox'] as $cbid => $cbvalue)
			{
				if($cbvalue==1)
				{
					$this->data['Quyetdinhttdoancb']['canbo_id']=$cbid;
					if($this->Quyetdinhttdoancb->saveAll($this->data))
					{}
					else
					{
						$this->Session->setFlash(__("Lỗi khi lưu đề nghị.",true),'error');
						$this->redirect(array('denghi',$chidoan_id));
					}
				}
			}
			$this->Session->setFlash(__("Đã lưu đề nghị trưởng thành đoàn.",true),'success');	
		}
		
		$chidoans= $this->Chidoansv->find('all',array('recursive'=>-1,'conditions'=>array('Chidoansv.id'=>$chidoan_id)));
		/// danh sach nhung can bo dang cho duyet
		$cbcho = $this->Quyetdinhttdoancb->find('list',array(
									'recursive'=>1,
									'fields'=>array('canbo_id'),
									'conditions'=>array('Ttdoancb.duyet'=>0,
														'Ttdoancb.chidoansv_id'=>$chidoan_id)));
		
		$dscb = $this->Canbo->Tgiachidoancb->find('list',array(
															'fields'=>array('canbo_id','canbo_id'),
															'conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id,
																				'NOT'=>array('canbo_id'=>$cbcho))));
		
		$canbos = $this->Canbo->find('all',array(
									'recursive'=>-1,
									'fields'=>array('id','mscb','hoten','ngaysinh'),
									'conditions'=>array('Canbo.id'=>$dscb,
                                                        'OR'=>array(array('ngayvaodoan !='=>NULL),array('ngayvaodoan !='=>'0000-00-00')),
														'OR'=>array(array('ngayttdoan '=>NULL),array('ngayttdoan'=>'0000-00-00')),
														'YEAR(CURRENT_DATE()) - YEAR(Canbo.ngaysinh)- (RIGHT(CURRENT_DATE(),5) < RIGHT(Canbo.ngaysinh,5)) >='=>30)));
		

		$this->set(compact('canbos','chidoan_id','chidoans'));
	} 

	function duyetttd()
	{
		if(!empty($this->data))
		{
			//luu duyet
			$this->Canbo->create();
			$this->Ttdoancb->create();
			foreach ($this->data['Quyetdinhttdoancb']['checkbox'] as $cbid => $cbvalue)
			{
				if($cbvalue==1)
				{
					//luu duyet
					$this->Quyetdinhttdoancb->id = $cbid;
					$this->Quyetdinhttdoancb->saveField('duyet', 1);
					
					//luy duyet cho de nghi 
					$this->Ttdoancb->id=$this->data['Quyetdinhttdoancb']['ttdoancb_id'][$cbid];
					$this->Ttdoancb->saveField('duyet',1);
					
					//luu ngay vao doan cua doan vien
					$this->Canbo->id=$this->data['Quyetdinhttdoancb']['canbo'][$cbid];
					$this->Canbo->saveField('ngayttdoan', date('Y-m-d',strtotime($this->data['Quyetdinhttdoancb']['ngayduyet'])));
					//$this->Sinhvien->saveField('noivaodoan', .'ĐHCT');
				}
			}
			$this->Session->setFlash(__("Đã lưu kết quả duyệt.",true),'success');	
		}
		
		$cd = $this->Ttdoancb->find('list',array('fields'=>array('chidoansv_id'),
												'conditions'=>array('duyet'=>0)));
		
		$chidoans= $this->Chidoansv->find('list',array(
													'fields'=>array('tenchidoan'),
													'conditions'=>array('id'=>$cd)));
		$this->set(compact('chidoans'));
		$chidoan = 0;
		$this->set(compact('chidoan'));
		$this->Quyetdinhttdoancb->recursive=0;
		$this->paginate = array('conditions'=>array('Ttdoancb.duyet'=>0),
								'limit'=>100,
								'order'=>array('Ttdoancb.ngayttdoan'=>'DESC'));
		$this->set('quyetdinhttdoancbs',$this->paginate());
	}
	
	function updateds()
	{
		$this->layout = 'ajax';
		$chidoan = $this->data['Quyetdinhttdoancb']['chidoan'];
		$this->Quyetdinhttdoancb->recursive=0;
		if(!empty($chidoan))
			$this->paginate = array('conditions'=>array('Ttdoancb.duyet'=>0,'Ttdoancb.chidoansv_id'=>$chidoan),
								'limit'=>100,
								'order'=>array('Ttdoancb.ngayttdoan'=>'DESC'));
		else
		{
			$this->paginate = array('conditions'=>array('Ttdoancb.duyet'=>0),
								'limit'=>100,
								'order'=>array('Ttdoancb.ngayttdoan'=>'DESC'));
			$chidoan = 0;
		}
		$this->set(compact('chidoan'));
		$this->set('quyetdinhttdoancbs',$this->paginate());
	}

	function xuatexcel($chidoan=0)
	{
		$this->layout = '';
		$this->Quyetdinhttdoancb->recursive=0;
		if($chidoan!=0)
			$this->paginate = array('conditions'=>array('Ttdoancb.duyet'=>0,'Ttdoancb.chidoansv_id'=>$chidoan),
								'limit'=>100,
								'order'=>array('Ttdoancb.ngayttdoan'=>'DESC'));
		else
			$this->paginate = array('conditions'=>array('Ttdoancb.duyet'=>0),
								'limit'=>100,
								'order'=>array('Ttdoancb.ngayttdoan'=>'DESC'));
			
			
		$this->set('quyetdinhttdoancbs',$this->paginate());
	}
}
